package com.kedacom.demo.redis.jedis;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import redis.clients.jedis.Jedis;

/**
 * RedisTasks
 * @author luocanfeng
 * @date 2015年8月6日
 */
public class SimpleStringWriteTask implements Runnable {

	private Logger logger = LoggerFactory.getLogger(getClass());

	private volatile long start = 0;
	private String name;
	private int runTimes = 1;

	public SimpleStringWriteTask() {
		if (start == 0) {
			synchronized (this) {
				start = System.currentTimeMillis();
			}
		}
	}

	public SimpleStringWriteTask(String name, int runTimes) {
		this();
		this.name = name;
		this.runTimes = runTimes;
	}

	@Override
	public void run() {
		Jedis jedis = RedisUtils.getJedis();
		for (int i = 1; i <= runTimes; i++) {
			String string = name + "_" + i;
			jedis.set(string, string);
			// jedis.del(string);
			if (i % 500 == 0) {
				logger.debug("{}, current proccess is {}", name, i);
			}
		}
		RedisUtils.returnJedis(jedis);

		logger.debug("{} finished, cost {}ms.", name, System.currentTimeMillis() - start);
	}

}
